package com.ruoyi.info.controller;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.info.domain.CmsApp;
import com.ruoyi.info.service.ICmsAppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/app")
public class AppController extends BaseController {

    @Autowired
    private ICmsAppService cmsAppService;

    @RequestMapping("/uploadCallback")
    @ResponseBody
    public AjaxResult uploadCallback(@RequestParam Map<String, Object> param){
        logger.info("uploadCallback:{}", param);
        System.out.println("param: " + param);
        System.out.println("param: " + param.get("report"));

        String batch_id = Convert.toStr(param.get("batch_id"));
        String id = Convert.toStr(param.get("id"));
        String file_md5 = Convert.toStr(param.get("file_md5"));
        String report = Convert.toStr(param.get("report"));

        if(StringUtils.isEmpty(batch_id)) {
            logger.warn("---收到的batch_id为空，无法继续...");
            return AjaxResult.success();
        }


        if(StringUtils.isEmpty(report)) {
            logger.warn("---收到的report为空，无法继续...\n{}", report);
            return AjaxResult.success();
        }

        try {
            JSONObject jo = JSON.parseObject(report);

            if(jo.get("BasicAppInfo") != null) {

                JSONObject appInfo = (JSONObject) jo.get("BasicAppInfo");

                String PackageName = Convert.toStr(appInfo.get("PackageName"));
                String VersionName = Convert.toStr(appInfo.get("VersionName"));
                String FileSize = Convert.toStr(appInfo.get("FileSize"));
                String AppName = Convert.toStr(appInfo.get("AppName"));

                String bundle_id = Convert.toStr(appInfo.get("bundle_id"));
                String bundle_version = Convert.toStr(appInfo.get("bundle_version"));
                String size = Convert.toStr(appInfo.get("size"));
                String display_name = Convert.toStr(appInfo.get("display_name"));

                CmsApp exist = cmsAppService.selectCmsAppByBatchId(batch_id);
                if(exist != null) {
                    if("apk".equals(exist.getExt1())) {
                        exist.setPackName(PackageName);
                        exist.setVersion(VersionName);
                        exist.setSize(FileSize);
                        exist.setAppName(AppName);
                    } else {
                        exist.setPackName(bundle_id);
                        exist.setVersion(bundle_version);
                        exist.setSize(size);
                        exist.setAppName(display_name);
                    }

                    exist.setRelativeId(id);
                    exist.setExt2(report);
                    cmsAppService.updateCmsApp(exist);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return AjaxResult.success();
    }
}
